import { Collection, EntitySchema, Ref } from '@mikro-orm/core';
import { OfficeNews } from './OfficeNews';
import { Users } from './Users';

export class OfficePosts {
  id!: string;
  createdAt?: Date;
  removedAt?: Date;
  title?: string;
  html?: string;
  creator?: Ref<Users>;
  digest?: string;
  contentSourceUrl?: string;
  thumbMediaId?: string;
  showCoverPic?: number;
  url?: string;
  thumbUrl?: string;
  isDeleted?: number = 0;
  newsAsPostsPivots = new Collection<OfficeNews>(this);
}

export const OfficePostsSchema = new EntitySchema({
  class: OfficePosts,
  properties: {
    id: { primary: true, type: 'string', columnType: 'bigint' },
    createdAt: { type: 'Date', length: 0, nullable: true },
    removedAt: { type: 'Date', length: 0, nullable: true },
    title: { type: 'string', length: 255, nullable: true },
    html: { type: 'string', columnType: 'text', nullable: true },
    creator: {
      reference: 'm:1',
      entity: () => Users,
      ref: true,
      nullable: true,
      index: 'creator_id',
    },
    digest: { type: 'string', columnType: 'text', nullable: true },
    contentSourceUrl: { type: 'string', length: 500, nullable: true },
    thumbMediaId: { type: 'string', length: 500, nullable: true },
    showCoverPic: { type: 'number', nullable: true },
    url: { type: 'string', length: 500, nullable: true },
    thumbUrl: { type: 'string', length: 500, nullable: true },
    isDeleted: { type: 'number', nullable: true, default: 0 },
    newsAsPostsPivots: { reference: 'm:n', entity: () => OfficeNews, mappedBy: 'postsPivots' },
  },
});
